package com.atguigu.flow.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.atguigu.flow.entity.LoginBO;
import com.atguigu.flow.entity.vo.TokenInfoBO;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public
class Gettoken {


    public  static JSONObject apiput(String token,String url,JSONObject sourjson) throws IOException {
        JSONObject jsonresult=new JSONObject();
        CloseableHttpResponse response1 = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        List<NameValuePair> paramList = new ArrayList<>();
        //String url="https://apis-sandbox.fedex.com/address/v1/addresses/resolve";
        HttpPut  httpPost = new HttpPut(url);
        httpPost.setEntity(new StringEntity(sourjson.toJSONString(), StandardCharsets.UTF_8));
        httpPost.setHeader("Content-type","application/json");
        httpPost.setHeader("X-locale", "en_US");
        //String token="eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJDWFMtVFAiXSwiUGF5bG9hZCI6eyJjbGllbnRJZGVudGl0eSI6eyJjbGllbnRLZXkiOiJsNzg4NmMzNjAwZWVhNTRkYjFiZGZhMTdhOGI2NzNkODY0In0sImF1dGhlbnRpY2F0aW9uUmVhbG0iOiJDTUFDIiwiYWRkaXRpb25hbElkZW50aXR5Ijp7InRpbWVTdGFtcCI6IjE0LUF1Zy0yMDIzIDAxOjM1OjQ5IEVTVCIsImdyYW50X3R5cGUiOiJjbGllbnRfY3JlZGVudGlhbHMiLCJhcGltb2RlIjoiU2FuZGJveCIsImN4c0lzcyI6Imh0dHBzOi8vY3hzYXV0aHNlcnZlci1zdGFnaW5nLmFwcC5wYWFzLmZlZGV4LmNvbS90b2tlbi9vYXV0aDIifSwicGVyc29uYVR5cGUiOiJEaXJlY3RJbnRlZ3JhdG9yX0IyQiJ9LCJleHAiOjE2OTE5OTg1NDgsImp0aSI6IjY1YjVlZDUwLThiZDctNGFiNC1iY2U4LThmYzE3NTJmZDUzNCJ9.kC8HEaYbjjVzOA365DpAn4lXHbuhgyabr4J62rLwiGtKxc9fOPNU9MTOggjJjd0eDlLqSTKeqOIOPnQHsyz_L7mJlHHKHAgpRC_nnghARf-rE741nTA_u_NezYv26Efn1ZwgLFYy5OQv-t7Hj0rr-lKpgM2xNDybxc27FRQV1Nm-jIWUwIq9Vsj0no9K18aV9ij3I-JcA3flrrSoym0cNpy1uyt9SH2BY8gnrh3U__W6Snp2r7TyoXD4LBszXBGbXdxYaXnGiOMqXSOkzl7bzEv8ocSe6ZMxvW9IpYFoe4v7ZzWvnLcH40SdsQ5Klaqwsrk0mPFikXEXTXtpkug4GGUVwV1NY50qkjguztlmHjuieKDTZ2cuv4ea_I2U8aO2abVLoNo_chR0OZY6RE4fyXiDctPLH7QWfTGsf0TdBVH9v2jLP77VwKDru-0aSj1nG1F7O-ICL8xh65HpATkA0AqNbvUkjkDmTZVmcT4mIQ9Ohigyfk2HDE9z3dZaq4097ctiliY1mLcbWrPfuShg9JImqtxBYJ7QaxywBZNMtAgNDAw19-o5OLuMWCow07QQtOCiuyl2Np6P1UgPAexQu9UqqnVjeTdT_nz-oJBbpqt_Or3Mtj7S_ckeILiNIEiVCHaIzvrdg7XGOt0HB7mdugPkVE2DguNzTdqgb7_ZXCQ";
        token="Bearer "+token;
        //System.out.println("token/"+token);
        httpPost.setHeader("Authorization", token);
        response1 = httpclient.execute(httpPost);

        HttpEntity entity = response1.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            String body = EntityUtils.toString(entity, "UTF-8");
            jsonresult= JSONObject.parseObject(body);


            //System.out.println(body);

        }
        return jsonresult;
    }


    public  static JSONObject apipost(String token,String url,JSONObject sourjson) throws IOException {
        JSONObject jsonresult=new JSONObject();
        CloseableHttpResponse response1 = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        List<NameValuePair> paramList = new ArrayList<>();
        //String url="https://apis-sandbox.fedex.com/address/v1/addresses/resolve";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(sourjson.toJSONString(), StandardCharsets.UTF_8));
        httpPost.setHeader("Content-type","application/json");
        httpPost.setHeader("X-locale", "en_US");
        //String token="eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJDWFMtVFAiXSwiUGF5bG9hZCI6eyJjbGllbnRJZGVudGl0eSI6eyJjbGllbnRLZXkiOiJsNzg4NmMzNjAwZWVhNTRkYjFiZGZhMTdhOGI2NzNkODY0In0sImF1dGhlbnRpY2F0aW9uUmVhbG0iOiJDTUFDIiwiYWRkaXRpb25hbElkZW50aXR5Ijp7InRpbWVTdGFtcCI6IjE0LUF1Zy0yMDIzIDAxOjM1OjQ5IEVTVCIsImdyYW50X3R5cGUiOiJjbGllbnRfY3JlZGVudGlhbHMiLCJhcGltb2RlIjoiU2FuZGJveCIsImN4c0lzcyI6Imh0dHBzOi8vY3hzYXV0aHNlcnZlci1zdGFnaW5nLmFwcC5wYWFzLmZlZGV4LmNvbS90b2tlbi9vYXV0aDIifSwicGVyc29uYVR5cGUiOiJEaXJlY3RJbnRlZ3JhdG9yX0IyQiJ9LCJleHAiOjE2OTE5OTg1NDgsImp0aSI6IjY1YjVlZDUwLThiZDctNGFiNC1iY2U4LThmYzE3NTJmZDUzNCJ9.kC8HEaYbjjVzOA365DpAn4lXHbuhgyabr4J62rLwiGtKxc9fOPNU9MTOggjJjd0eDlLqSTKeqOIOPnQHsyz_L7mJlHHKHAgpRC_nnghARf-rE741nTA_u_NezYv26Efn1ZwgLFYy5OQv-t7Hj0rr-lKpgM2xNDybxc27FRQV1Nm-jIWUwIq9Vsj0no9K18aV9ij3I-JcA3flrrSoym0cNpy1uyt9SH2BY8gnrh3U__W6Snp2r7TyoXD4LBszXBGbXdxYaXnGiOMqXSOkzl7bzEv8ocSe6ZMxvW9IpYFoe4v7ZzWvnLcH40SdsQ5Klaqwsrk0mPFikXEXTXtpkug4GGUVwV1NY50qkjguztlmHjuieKDTZ2cuv4ea_I2U8aO2abVLoNo_chR0OZY6RE4fyXiDctPLH7QWfTGsf0TdBVH9v2jLP77VwKDru-0aSj1nG1F7O-ICL8xh65HpATkA0AqNbvUkjkDmTZVmcT4mIQ9Ohigyfk2HDE9z3dZaq4097ctiliY1mLcbWrPfuShg9JImqtxBYJ7QaxywBZNMtAgNDAw19-o5OLuMWCow07QQtOCiuyl2Np6P1UgPAexQu9UqqnVjeTdT_nz-oJBbpqt_Or3Mtj7S_ckeILiNIEiVCHaIzvrdg7XGOt0HB7mdugPkVE2DguNzTdqgb7_ZXCQ";
        token="Bearer "+token;
        //System.out.println("token/"+token);
        httpPost.setHeader("Authorization", token);
        response1 = httpclient.execute(httpPost);

        HttpEntity entity = response1.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            String body = EntityUtils.toString(entity, "UTF-8");
             jsonresult= JSONObject.parseObject(body);


            //System.out.println(body);

        }
        return jsonresult;
    }
    public static TokenInfoBO  get_token(String url, String username, String password) throws IOException {
        TokenInfoBO tokenInfoBO=new TokenInfoBO();
        CloseableHttpResponse response1 = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();


        //String url="http://localhost:8020/service-flow/api/login";
        JSONObject object = new JSONObject();
        LoginBO bo = new LoginBO();
        bo.setUserName(username);
        //2023-05-10 23:48:51loie_api25D55AD283AA400AF464C76D713C07AD
        bo.setLoginDate(DateUtil.DateTimeToString(new Date()));
        //String password="123456";
        password = SecureUtil.MD5Encode(password);
        String SLAT = "loie_api";

        String comparePasswd = bo.getLoginDate() + SLAT + password;
        comparePasswd = SecureUtil.MD5Encode(comparePasswd);
        bo.setPassword(comparePasswd);
        String jsonString = JSON.toJSONString(bo);

        System.out.println(jsonString);
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new StringEntity(jsonString, StandardCharsets.UTF_8));
        //httpPost.setHeader("Content-Type", "application/text;charset=utf8");
        httpPost.setHeader("Content-type", "application/json");


        //httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        response1 = httpclient.execute(httpPost);

        HttpEntity entity = response1.getEntity();
        String str = "未获取token";
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            String body = EntityUtils.toString(entity, "UTF-8");
            JSONObject jsonresult = JSONObject.parseObject(body);
            if (jsonresult.containsKey("success")) {
                if (jsonresult.getString("success").equals("true")) {
                    JSONObject json1 = jsonresult.getJSONObject("data");
                    tokenInfoBO.setToken(json1.getString("token"));
                    tokenInfoBO.setPublickey(json1.getString("publicKey"));
                    return tokenInfoBO;
                }


                //System.out.println(body);

            }
        }
            return tokenInfoBO;

        }


}
